Component({
    properties: {
        searchPlaceholder: {
            type: String,
            value: "请输入昵称"
        },
        hostList: {
            type: null,
            value: null,
            observer: function(t, e) {
               if(t){
                 var list_key = Object.keys(t)
                 this.setData({
                   currentIndex: list_key[0]
                 })
                 this._calculateHeight()
               }
            }
        },
        zhuList: {
            type: null,
            value: [],
            observer: function(t, e) {}
        },
        userList:{
            type: null,
            value: [],
        },
    },
    data: {
        character: [ "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "X", "Y", "Z" ],
        toView: "A",
        listHeight: [],
        currentIndex: "A",
        windowheight:0,
        safeAreaBottom: 0
    },
    attached: function() { 
      let systemInfoSync = wx.getSystemInfoSync();
      //rpx
      let windowheight = systemInfoSync.windowHeight * (750 / systemInfoSync.windowWidth)  
      let safeAreaBottom = (systemInfoSync.screenHeight - systemInfoSync.safeArea.bottom) * (750 / systemInfoSync.windowWidth)
      this.setData({
        windowheight,
        safeAreaBottom
      })

    },
    ready: function () {},
    methods: {
        setId: function(t) {
            var e = t.currentTarget.dataset.id;
           
            this.setData({
                toView: e
            });
        },
        // 选择用户
        addUser: function(t){
            var uid = t.currentTarget.dataset.uid;
            var nickname = t.currentTarget.dataset.nickname;
            var userList = this.data.userList;
            let flag = 0
            //删除元素
            userList.forEach((item,index)=>{
                 if(item.uid == uid){
                  userList.splice(index,1);
                  flag = 1;
                 }
            })
            //添加元素
            if(!flag){
              userList.push({uid,nickname});
            }
            //
            var hostList = this.data.hostList;
            var index = t.currentTarget.dataset.index;
            var zimu = t.currentTarget.dataset.zimu;
            if(hostList[zimu][index].flag){
              hostList[zimu][index].flag = false
            }else{
              hostList[zimu][index].flag = true
            }
            this.setData({
              userList,
              hostList
            })
        },
        //返回保存
        saveuser(){
          var hostList = this.data.hostList;
          var userList = this.data.userList;
          this.triggerEvent("setuser", {
            userList,
            hostList
           });
        },
        search: function(t) {
            var e = this, i = t.detail.value, a = e.data.zhuList;
            if (i) {
                var r = {};
                for (var n in a) r[n] = [], a[n].forEach(function(t, e) {
                    t.nickname.indexOf(i) > -1 && r[n].push(t);
                });
                e.setData({
                    hostList: r
                });
            } else e.setData({
                hostList: a
            });
        },
        setuid: function(t) {
            var e = t.currentTarget.dataset.nickname, i = t.currentTarget.dataset.uid;
            this.triggerEvent("setuid", {
                nickname: e,
                uid: i
            });
        },
        scroll: function(t) {
            var e = t.detail.scrollTop;
            this.scrollY(e);
        },
        scrollY: function(t) {
          var hostList = this.data.hostList
          var list_key = Object.keys(hostList)
          const listHeight = this.data.listHeight
          if (0 == t || t < 0) {
            this.setData({
              currentIndex: list_key[0]
            });
            return;
          }

          // 在中间部分滚动
          for (let i = 0; i < listHeight.length - 1; i++) {
            let height1 = listHeight[i]
            let height2 = listHeight[i + 1]
            if (t >= height1 && t < height2) {
              this.setData({
                currentIndex: list_key[i]
              })
              return;
            }
          }
          // 当滚动到底部，且-newY大于最后一个元素的上限
          this.setData({
            currentIndex: list_key[listHeight.length - 2],
          })
        },
        _calculateHeight: function() {
            var t = [], e = this, i = 0;
            t.push(i), this.createSelectorQuery().selectAll(".zimuzhu").boundingClientRect(function(e) {
                for (var a = e, r = a.length, n = 0; n < r; n++) i += a[n].height, t.push(i);
            }).exec();
            var a = setInterval(function() {
                t != [ 0 ] && (e.setData({
                    listHeight: t
                }), clearInterval(a));
            }, 1e3);
        }
    }
});